#include<iostream>
using namespace std;
#include<cmath>

#include"library.h"

double g1(int n, double *x){
        double z = 0;
        for(int i=0;i<n;i++) z += x[i];
        return z;
}

/*
 *  N = N0 e^{-t/tau}
 *  ln(N/N0) = - t/tau
 *  t_{1/2} = tau * ln2
 */
int main(){

	int i,j,n=1, m=1,N=8;
	double ( *g[1] )(int, double * )={g1};

	double **x_data = new double * [N];
	for(i=0;i<N;i++) x_data[i] = new double [n];
	double *y_data = new double [N];

	double y_data_source[8] = { log(1), log(0.71), log(0.50), log(0.37), log(0.26), log(0.17), log(0.11), log(0.09) };

	for(i=0;i<N;i++){
		x_data[i][0]= i*10;
		y_data[i] = y_data_source[i];
	}

	double *a = new double [m];
	cout<<"RMSD="<<LeastSquare(N, x_data, y_data, n, m, a, g, 0.01 )<<endl;
	cout<<"tau = "<< - 1/a[0]<<endl;
	cout<<"half life = tau * ln2 = "<< - 1/a[0] * log(2)<<" s" <<endl;

	delete [] a;
	delete [] y_data;
	for(i=0;i<N;i++)delete [] x_data[i]; delete [] x_data;

	return 0;
}
